import nl.siegmann.epublib.domain.*;
import nl.siegmann.epublib.epub.EpubReader;
import nl.siegmann.epublib.epub.EpubWriter;

import java.io.*;
import java.util.List;

public class product{
    public static String file(String path,boolean flag,String newname){
        File file = new File(path);
        String parpath = file.getParent();
        InputStream in = null;
        try {
            //从输入流当中读取epub格式文件
            EpubReader reader = new EpubReader();
            in = new FileInputStream(file);
            Book book = reader.readEpub(in);

            //获取到书本的内容资源
            List< Resource > contents = book.getContents();
            int length = contents.size();
            int index = 0;
            /**
             * 相当于对OEBPS文件夹下的Text文件夹下的所有xhtml中的中文进行转换
             */
            while ( length > 0 ) {
                Reader r = contents.get(index).getReader();
                String ss;
                String str = "";
                BufferedReader bin_2 = new BufferedReader(r);
                while ( (ss = bin_2.readLine()) != null ) {
                    str = str + ss + "\n";
                }
                r.close();
                str = change.test1(str,flag);
                contents.get(index).setData(str.getBytes("UTF8"));
                index++;
                length--;
            }

            //获取到书本的目录资源
            TableOfContents tableOfContents = book.getTableOfContents();
            //获取到目录对应的资源数据
            List< TOCReference > tocReferences = tableOfContents.getTocReferences();
            /**
             *  对每个小节里的中文进行转换
             */
            for ( TOCReference tocReference : tocReferences ) {
                Resource resource = tocReference.getResource();
                //data就是资源的内容数据，可能是css,html,图片等等
                byte[] data = resource.getData();
                // 获取到内容的类型  css,html,还是图片
                MediaType mediaType = resource.getMediaType();
                tocReference.setTitle(change.test1(tocReference.getTitle(),flag));
            }

            /**
             * 对文件content.opf中的中文进行转换
             */
            Resource opf = book.getOpfResource();
            Reader r = opf.getReader();
            String ss="";
            String str = "";
            BufferedReader bin_2 = new BufferedReader(r);
            while ( (ss = bin_2.readLine()) != null ) {
                str = str + ss + "\n";
            }
            r.close();
            str = change.test1(str,flag);
            opf.setData(str.getBytes("UTF8"));

            /**
             *  相当于对toc.ncx中的章节中的中文进行转换
             */
            List<String> stringList=book.getMetadata().getTitles();
            for ( int i=0;i<stringList.size();i++ ){
                stringList.set(i,change.test1(stringList.get(i),flag));
            }
            book.getMetadata().setTitles(stringList);

            /**
             * 输出数据到一个新的equb文件
             */


            //System.out.println("newEpubWriter before");
            EpubWriter epubWriter = new EpubWriter();
            //System.out.println("epubWriter:"+epubWriter);
            String newfile = parpath + "\\" + newname;
            OutputStream ouput = new FileOutputStream(newfile);
            epubWriter.write(book, ouput);

            ouput.close();
            return "文件转换成功";
        } catch ( Exception e ) {
            e.printStackTrace();
            return "文件转换失败："+e.getMessage();
        } finally {
            //一定要关闭资源
            try {
                if ( in != null ) {
                    in.close();
                }
            } catch ( IOException e ) {
                e.printStackTrace();
            }
        }
    }
}
